//
//  MapAnnotationView.m
//  Travelmemory
//
//  Created by lanou3g on 16/3/29.
//  Copyright © 2016年 ABCD. All rights reserved.
//

#import "MapAnnotationView.h"

@interface MAAnnotationView()

@end

@implementation MapAnnotationView
#define kCalloutWidth       100.0
#define kCalloutHeight      100.0

- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
        
    }
    return self;
}

#pragma mark - 重写选中方法setSelected。选中时新建并添加calloutView，传入数据；非选中时删除calloutView。
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (self.selected == selected)
    {
        return;
    }
    
    if (selected)
    {
        if (self.calloutView == nil)
        {
            self.calloutView = [[MapView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
            self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
                                                  -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
        }
        @autoreleasepool {
            self.calloutView.image = [UIImage imageNamed:@"cat.png"];
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.picArray[0]]];
            // 压缩图片
            self.calloutView.image = [self scaleToSize:[UIImage imageWithData:data] size:CGSizeMake(100, 100)];
            [self.picArray removeObjectAtIndex:0];
            data = nil;
        }
        dispatch_async(
                       
                       dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                       , ^{
                           self.calloutView.image = [UIImage imageNamed:@"cat.png"];
                           
                           NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
                           
                           NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                           
                           NSLog(@"files :%lu",(unsigned long)[files count]);
                           
                           for (NSString *p in files) {
                               
                               NSError *error;
                               
                               NSString *path = [cachPath stringByAppendingPathComponent:p];
                               
                               if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                                   
                                   [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                                   
                               }
                           }
                       });
        
        [self addSubview:self.calloutView];
    }
    else
    {
        [self.calloutView removeFromSuperview];
    }
    
    [super setSelected:selected animated:animated];
}

#pragma mark - 压缩图片
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0,0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    //返回新的改变大小后的图片
    return scaledImage;
}





@end
